<?php 

Class adController extends appController
{
	/**
	 * 显示版块列表
	 */
	public function index()
	{
		$page = yf_empty(intval($_GET['page']), 1, true);
		$ad_list = Model('ad')->ad_list($page);
		$this->assign('ad_list', $ad_list['data']);
		$this->assign('user_list_page', $ad_list['pagelink']);
		$this->display('index.html');
	}
	
	/**
	 * 删除版块
	 */
	public function del_group()
	{
		$this->admin_priv('delgroup', 2);
		$id = yf_empty($_REQUEST['id'], '', true);
		if(is_array($id))
		{
			foreach($id as $k => $v)
			{
				$result = Model('ad')->del_group($v);
			}
		}
		else
		{
			$result = Model('ad')->del_group($id);
		}
		$this->showMsg($result);
	}
	
	/**
	 * 删除版块详细内容
	 */
	public function del_content()
	{
		$this->admin_priv('delimage', 2);
		$id = yf_empty($_REQUEST['id'], '', true);
		if(is_array($id))
		{
			foreach($id as $k => $v)
			{
				$result = Model('ad')->del_img($v);
			}
		}
		else
		{
			$result = Model('ad')->del_img($id);
		}
		$this->showMsg($result);
	}
	
	/**
	 * 版块内容排序
	 */
	public function ordering()
	{
		$this->admin_priv('orderimage');
		$id = yf_empty(intval($_POST['id']), '', true);
		$val = yf_empty(intval($_POST['val']), '', true);
		$result = Model('ad')->ordering($val, $id);
		print_r(json_encode($result));
	}
	
	/**
	 * 显示版块详情列表
	 */
	public function content_list()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$flash_id = yf_empty(intval($_GET['bid']), '', true);
		$img_list = Model('ad')->content_list($flash_id, $page);
		$this->assign('flash_id', $flash_id);
		$this->assign('ad_list', $img_list['data']);
		$this->assign('user_list_page', $img_list['pagelink']);
		$this->display('content_list.html');
	}
	
	/**
	 * 显示版块编辑页面
	 */
	public function edit_group()
	{
		$this->admin_priv('editgroup', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$ad_group = Model('ad')->getGroup($id);
		$this->assign('ad_group', $ad_group);
		$this->assign('id', $id);
		$this->assign('action','update_group');
		$this->display('group.html');
	}
	
	/**
	 * 更新版块信息
	 */
	public function update_group()
	{
		$this->admin_priv('editgroup');
		$id = yf_empty($_POST['id'], '', true);
		$info['title'] = yf_empty($_POST['title'], Lang('标题不能为空'));
		$info['description'] = yf_empty($_POST['description'], '', true);
		$info['code'] = yf_empty($_POST['code'], '版块位置不能为空');
		
		$info['modified'] = yf_time();
		
		$result=array('error'=>1,'content'=>'');
		
		if(Model('ad')->check_code($info['code'], $id))
		{
			if(Model('ad')->update_group($info, $id))
			{
				$result['error'] = 0;
				$result['content'] = Lang('SUCCESS_EDIT');
			}
			else
			{
				$result['content'] = Lang('INVALID_OPERATION');
			}
		}
		else
		{
			$result['content'] = Lang('CODE_REPEATING');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 添加版块信息
	 */
	public function add_group()
	{
		$this->admin_priv('addgroup', 2);
		$this->assign('action', 'create_group');
		$this->display('group.html');
	}
	
	/**
	 * 插入版块信息
	 */
	public function create_group()
	{
		$this->admin_priv('addgroup');
		$info['title'] = yf_empty($_POST['title'], Lang('标题不能为空'));
		$info['description'] = yf_empty($_POST['description'], "", true);
		$info['code'] = yf_empty($_POST['code'], "版块位置不能为空");
		$info['created'] = yf_time();
		$result=array('error'=>1,'content'=>'');
		
		if(Model('ad')->check_code($info['code']))
		{
		  	if($ad_id = Model('ad')->insert($info, 'block'))
		  	{
		  	   	$result['error'] = 0;
			  	$result['content'] = Lang('SUCCESS_ADD');
		  	}
		  	else
		  	{
				$result['content'] = Lang('INVALID_OPERATION');
		  	}
		}
		else 
		{
			$result['content'] = Lang('CODE_REPEATING');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示版块内容添加页
	 */
	public function add_content()
	{
		$this->admin_priv('addimage', 2);
		$flash_id = yf_empty($_GET['bid'], '', true);
		$this->assign('bid', $flash_id);
		$this->assign('action', 'sumbit_content');
		$this->display('content.html');
	}
	
	/**
	 * 显示版块内容编辑页
	 */
	public function edit_content()
	{
		$this->admin_priv('editimage', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$ad_img = Model('ad')->getImg($id);
		$this->assign('ad', $ad_img);
		$this->assign('id', $id);
		$this->assign('action', 'update_content');
		$this->display('content.html');
	}
	
	/**
	 * 更新版块详细内容
	 */
	public function update_content()
	{
		$this->admin_priv('editimage');
		$en_title = yf_empty($_POST['en_title'], '', true);
		$title = yf_empty($_POST['title'], Lang('标题不能为空'));
		$info['title'] = isset($_POST['title']) ? CH_SC.$title.DIG_EC.EN_SC.$en_title.DIG_EC : '';
		$en_des = yf_empty($_POST['en_description'], '', true);
		$info['description'] = isset($_POST['description']) ? CH_SC.$_POST['description'].DIG_EC.EN_SC.$en_des.DIG_EC : '';
		$info['url'] = yf_empty($_POST['url'], '#', true);
		$info['pic_path'] = yf_empty($_POST['pic_path'], '', true);
		$id = yf_empty(intval($_POST['id']), '', true);
		$info['modified'] = yf_time();
		$data = array('error' => 1, 'content' => '');
		
		$result = Model('ad')->update($info, array('table'=>'block_content','id'=>$id));
			
		if($result)
		{
		   	$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	/**
	 * 添加版块内容
	 */
	public function sumbit_content()
	{
		$this->admin_priv('addimage');
		$en_title = yf_empty($_POST['en_title'], '', true);
		$title = yf_empty($_POST['title'], Lang('标题不能为空'));
		$info['title'] = isset($_POST['title']) ? CH_SC.$title.DIG_EC.EN_SC.$en_title.DIG_EC : '';
		$en_des = yf_empty($_POST['en_description'], '', true);
		$info['description'] = isset($_POST['description']) ? CH_SC.$_POST['description'].DIG_EC.EN_SC.$en_des.DIG_EC : '';
		$info['url'] = yf_empty($_POST['url'], '#', true);
		$info['ordering'] = yf_empty(intval($_POST['ordering']), 0, true);
		$info['block_id'] = yf_empty($_POST['block_id'], '', true);
		$info['pic_path'] = yf_empty($_POST['pic_path'], '', true);
		
		$data = array('error' => 1, 'content' => Lang('INVALID_OPERATION'));
		
		$info['created'] = yf_time();
		$result=Model('ad')->add_images($info);
		   
		if($result)
		{
			$data['error'] = 0;
			$data['content'] = Lang('SUCCESS_ADD');
		}
		else
		{
			$data['content'] = "添加失败";
		}
		$this->showMsg($data);
	}
}